global time 1994
global lastyear 2019

** Load savings data **
u year lnr hhid grunnbelop edlevel wealth_a debt*_a bonds_a carboat_a LTV_debt_a deposits*_a stocks*_a mfund_a housing*_a b_year cpi earned_income_a transfers_a age *PB* beq_* parent* death cap_inc* cap_exp* nt_* net_private_debt* cons_* spending_* using $data/welfare_9319 if death != 1 & b_year != . & year <= $lastyear, clear

bys lnr (year): gen last = _N == _n
replace last = 0 if year == $lastyear

tsset lnr year

********************************************************************************
* DEAL WITH BEQUEST EVENTS: in the event of transfer in inheritance data, set 
* parents to zero, and adjust recipients to make everything sum to zero

merge 1:1 lnr year using $data/apr_bequest_rec_event, keep(match master) nogen keepusing(nt*_rec)
gen _temp1 = nt_housing_PB_a_rec != .
bys lnr: egen _temp2 = sum(_temp1)
gen beq_rec = _temp2 != 0
cap drop _temp1 _temp2

foreach var in stocks_PB housing_PB equity_PB stocks mfund {
	replace nt_`var'_a = nt_`var'_a + nt_`var'_a_rec if nt_`var'_a_rec != .
}
cap drop *_rec

merge m:1 lnr year using $data/apr_bequest_give_event, keep(match master) nogen keepusing(beq_event)
foreach var in stocks_PB housing_PB equity_PB stocks mfund {
	replace nt_`var'_a = 0 if beq_event == 1
}

cap drop beq_event


********************************************************************************

gen origSample = 1
tsfill, full
bys lnr: egen _temp = max(b_year)
replace b_year = _temp
replace age = year - _temp
drop _temp

replace last = 0 if last == .

bys lnr (year): gen _temp = sum(last)
drop if _temp == 1 & last != 1
drop _temp 


replace wealth_a = housing_a + housing_PB_a - debt_a - debt_PB_a + bonds_a + net_private_debt_a + deposits_a + deposits_PB_a + mfund_a + stocks_a + stocks_PB_a + equity_PB_a if year >= 2005

* replace the fillins with zeros to not get missing
replace earned_income_a = 0 if earned_income_a == .
replace transfers_a = 0 if transfers_a == .
replace wealth_a = 0 if wealth_a == .

* fillin grunnbelop
bys year: egen medGrunn = median(grunnbelop)
replace grunnbelop = medGrunn if grunnbelop == .
drop medGrunn

gen net_nf_income = (earned_income_a + transfers_a)*0.65*(0.95^(year-1993))
gen min_income = 2*grunnbelop*(0.95^(year-1993)) if age >= 18
replace min_income = 0 if min_income == .

* make human wealth in 2019
replace net_nf_income = (earned_income_a + transfers_a + (1-0.95^20)/(1-0.95)*(earned_income_a + transfers_a))*0.65*0.95^(year-1993) if last == 1 | year == $lastyear
replace min_income = (2*grunnbelop + (1-0.95^20)/(1-0.95)*(2*grunnbelop))*0.65*0.95^(year-1993) if last == 1 | year == $lastyear
replace net_nf_income = min_income if min_income > net_nf_income
gen year2 = -year
bys lnr (year2): gen human_wealth_a = sum(net_nf_income)
bys lnr (year2): gen min_human_wealth = sum(min_income)
replace human_wealth_a = human_wealth_a*0.95^(1993-year)

drop year2 net_nf_income
rename wealth_a financial_wealth_a
gen total_wealth_a = financial_wealth_a + human_wealth_a
gen total_wealth_main_a = total_wealth_a


* winsorize at the bottom 5% in each cohort-year
qui bys b_year year: egen _bot5 = pctile(total_wealth_a), p(5)
replace total_wealth_a = _bot5 if total_wealth_a < _bot5
drop _bot5


********************************************************************************
global namelist housing housing_PB debt debt_PB  deposits deposits_PB stocks stocks_PB mfund bonds equity_PB net_private_debt stocks_nonreg


tsset lnr year

foreach v in earned_income transfers financial_wealth total_wealth total_wealth_main human_wealth $namelist LTV_debt cons_excl_housing {
	replace `v'_a = 0 if `v'_a == .
	replace `v'_a = `v'_a/1e3
	cap safedrop `v'
	rename `v'_a `v'
	rename `v', lower
}

foreach v in $namelist {
	replace nt_`v'_a = 0 if nt_`v'_a == .
	replace nt_`v'_a = nt_`v'_a/1e3
	cap safedrop nt_`v'
	rename nt_`v'_a nt_`v'
	rename nt_`v', lower
}




sort year
merge m:1 year using "$data/prices.dta", nogen keep(match)
merge m:1 year using "$data/kappa.dta", nogen keep(match)

cd  $programs
* requires gen_transactions.ado to be in the "programs" folder
gen_transactions


********************************************************************************
* save
save $data/apr_9319, replace

keep if origSample == 1
save $data/apr_orig_9319, replace



